package com.mobiusws.util;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class HeaderFooter extends PdfPageEventHelper
{
	Phrase[]	header	= new Phrase[2];
	int			pagenumber;

	public void onOpenDocument(PdfWriter writer, Document document)
	{
		header[0] = new Phrase("mobiusws report");
		header[1] = new Phrase("mobiusws report");
	}

	public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
	{
		header[0] = new Phrase("mobiusws report");
		header[1] = new Phrase("mobiusws report");
		pagenumber = 1;
	}

	public void onStartPage(PdfWriter writer, Document document)
	{
		pagenumber++;
	}

	public void onEndPage(PdfWriter writer, Document document)
	{
		Rectangle rect = writer.getBoxSize("pagebox");

		//write the footer
		Font FONT = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);
		ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber), FONT), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom()-30, 0);
	}
}
